// The following helper routines are from the Subtext WebLog project
///////////////////////////////////////////////////////////////////////////////////////////////////
// Subtext WebLog
// 
// Subtext is an open source weblog system that is a fork of the .TEXT
// weblog system.
//
// For updated news and information please visit http://subtextproject.com/
// Subtext is hosted at SourceForge at http://sourceforge.net/projects/subtext
// The development mailing list is at subtext-devs@lists.sourceforge.net 
//
// This project is licensed under the BSD license.  See the License.txt file for more information.
///////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Specialized;
using System.Data;
using System.Configuration;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace ITCrossing.Tracking
{
	public class HtmlHelper
	{


		private const int defaultTimeout = 60000;
		private static string referer = @"http://www.itcrossing.com/";
		private static readonly string userAgent = "ITCrossing metaPost"
			+ " (" + Environment.OSVersion + "; .NET CLR " + Environment.Version + ")";

		/// <summary>
		/// Parses the URI.
		/// </summary>
		/// <param name="uri">The URI.</param>
		/// <returns></returns>
		public static Uri ParseUri(string uri)
		{
			if (uri == null)
				return null;

			try
			{
				return new Uri(uri);
			}
			catch (UriFormatException)
			{
				return null;
			}
		}
		/// <summary>
		/// Creates an <see cref="HttpWebRequest" /> for the specified URL..
		/// </summary>
		/// <param name="url">The URL.</param>
		/// <returns></returns>
		public static HttpWebRequest CreateRequest(Uri url)
		{
			WebRequest req = WebRequest.Create(url);
			SetProxy(req);
			HttpWebRequest wreq = req as HttpWebRequest;
			if (null != wreq)
			{
				wreq.UserAgent = userAgent;
				wreq.Referer = referer;
				wreq.Timeout = defaultTimeout;
			}
			return wreq;
		}
		/// <summary>
		/// Sets the proxy on the request if a proxy is configured in Web.config.
		/// </summary>
		/// <param name="request"></param>
		public static void SetProxy(WebRequest request)
		{
			IWebProxy proxy = GetProxy();
			if (proxy != null)
				request.Proxy = proxy;
		}

		internal static IWebProxy GetProxy()
		{
			if (String.IsNullOrEmpty(ConfigurationManager.AppSettings["ProxyHost"]))
				return null;

			IWebProxy proxy;
			string proxyHost = ConfigurationManager.AppSettings["ProxyHost"];

			int proxyPort;
			if (int.TryParse(ConfigurationManager.AppSettings["ProxyPort"], out proxyPort))
			{
				proxy = new WebProxy(proxyHost, proxyPort);
			}
			else
			{
				proxy = new WebProxy(proxyHost);
			}
			if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["ProxyUsername"]))
			{
				proxy.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["ProxyUsername"], ConfigurationManager.AppSettings["ProxyPassword"]);
			}
			return proxy;
		}
		/// <summary>
		/// Returns a string collection of URLs within the specified text.
		/// </summary>
		/// <param name="text">The text.</param>
		/// <returns></returns>
		public static StringCollection GetLinks(string text)
		{
			StringCollection links = new StringCollection();
			string sPattern = @"(?:[hH][rR][eE][fF]\s*=)" +
				@"(?:[\s""']*)(?!#|[Mm]ailto|[lL]ocation.|[jJ]avascript|.*css|.*this\.)" +
				@"(.*?)(?:[\s>""'])";

			Regex r = new Regex(sPattern, RegexOptions.IgnoreCase);
			Match m;
			for (m = r.Match(text); m.Success; m = m.NextMatch())
			{
				if (m.Groups.ToString().Length > 0)
				{
					string link = m.Groups[1].ToString();
					if (!links.Contains(link))
					{
						links.Add(link);
					}
				}
			}
			return links;
		}
	}
}
